package com.example.ymm.modules1.service.impl.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.user.DbBook;
import com.example.ymm.modules1.entity.my.user.DbReadLog;
import com.example.ymm.modules1.entity.my.user.DbUserNote;
import com.example.ymm.modules1.mapper.my.user.DbReadLogMapper;
import com.example.ymm.modules1.service.dto.DbReadLogQueryCriteria;
import com.example.ymm.modules1.service.user.DbBookService;
import com.example.ymm.modules1.service.user.DbReadLogService;
import com.example.ymm.modules1.service.user.DbUserNoteService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class DbReadLogServiceImpl extends BaseServiceImpl<DbReadLogMapper, DbReadLog> implements DbReadLogService {

    @Autowired
    DbUserNoteService dbUserNoteService;

    @Autowired
    DbBookService dbBookService;

    @Override
    public TableList<DbReadLog> list(DbReadLogQueryCriteria criteria, YmPageDto pageDto) {
        IPage<DbReadLog> page = new Page<DbReadLog>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(DbBook.class, criteria);
        predicate.orderByDesc("update_time");
        baseMapper.selectPage(page, predicate);
        TableList<DbReadLog> tableList=new TableList(page);
        return tableList;
    }


    @Override
    public TableList<DbUserNote> getReadList(DbReadLogQueryCriteria criteria, YmPageDto ymPage) {
        getPage(ymPage);
        List<DbUserNote> readList = baseMapper.getReadList(criteria);
        for(int a=0;a<readList.size();a++){
//            DbUserNote userNote = readList.get(a);
//            DbUserNoteQueryCriteria criteria2=new DbUserNoteQueryCriteria();
//            criteria2.setUserId(userNote.getUserId());
//            criteria2.setBookId(userNote.getBookId());
//            DbUserNote newUserNote = dbUserNoteService.getNewUserNote(criteria2);
//            userNote.setCreateTime(newUserNote.getCreateTime());
//            userNote.setStartPage(newUserNote.getStartPage());
//            userNote.setEndPage(newUserNote.getEndPage());
        }

        PageInfo<DbUserNote> pageInfo = new PageInfo<>(readList);
        TableList<DbUserNote> tableList=new TableList<>(pageInfo);
        return tableList;
    }

    public List<DbReadLog> queryAll(DbReadLogQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(DbBook.class, criteria);
        predicate.orderByDesc("update_time");
        List<DbReadLog> result =baseMapper.selectList(predicate);
        return result;
    }


    @Override
    public void addReadLog(String bookId, String bookName) {
        String userId = getUserId();
        String userName = getUserName();

//        DbReadLogQueryCriteria criteria=new DbReadLogQueryCriteria();
//        criteria.setBookId(bookId);
//        criteria.setUserId(userId);
//        List<DbReadLog> dbReadLogs = queryAll(criteria);
//        if(UtilValidate.isEmpty(dbReadLogs)){
//
//        }else{
//            DbReadLog readLog = dbReadLogs.get(0);
//            readLog.setBookName(bookName);
//            saveOrUpdate(readLog);
//        }

        DbBook book = dbBookService.getById(bookId);
        if(UtilValidate.isNotEmpty(book)){
            if(UtilValidate.isNotEmpty(book.getNum())){
                book.setNum(book.getNum()+1);
            }else{
                book.setNum(1);
            }
            DbBook b=new DbBook();
            b.setBookId(bookId);
            b.setNum(book.getNum());
            dbBookService.updateById(b);
        }

        DbReadLog readLog=new DbReadLog();
        readLog.setBookId(bookId);
        readLog.setBookName(bookName);
        readLog.setUserId(userId);
        readLog.setUserName(userName);
        saveOrUpdate(readLog);
    }
}
